package cn.fl.spl.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ISplOpLogRecService;
import cn.fl.spl.transform.SplOpLogRecVOTransform;
import cn.fl.spl.vo.SplOpLogRecVO;
import cn.fl.spl.vo.SplVisitRecVO;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.mongo.facade.base.page.PaginationVo;
import fl.mongo.facade.spl.condition.SplOpLogRecQC;
import fl.mongo.facade.spl.constant.SplOpLogRecConstant;
import fl.mongo.facade.spl.dto.SplOpLogRecDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商拜访记录 控制层
 */
@Controller
@RequestMapping("/spl/splOpLogRec")
public class SplOpLogRecController extends BaseController {

	/**
	 * 数据字典
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	@Autowired
	private ISplOpLogRecService splOpLogRecService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET })
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("spl/splOpLogRec/splOpLogRecList");

		String dataId = request.getParameter("dataId");// 供应商ID
		String opObjTyp = request.getParameter("opObjTyp");
		view.addObject("dataId", dataId);
		view.addObject("opObjTyp", opObjTyp);

		return view;
	}
	
	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {

		ListPageVO<SplOpLogRecVO> listPageVO = new ListPageVO<SplOpLogRecVO>();
		
		String dataId = request.getParameter("dataId");
		String opObjTyp = request.getParameter("opObjTyp");
		
		if (StringUtils.isEmpty(dataId)) {
			BaseLogUtils.newWebLogger("操作对象ID为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
			return null;
		}
		if (StringUtils.isEmpty(opObjTyp)) {
			BaseLogUtils.newWebLogger("操作对象类型为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
			return null;
		}
		
		PaginationVo paginationVo = new PaginationVo();
		SplOpLogRecQC condition = new SplOpLogRecQC();
		condition.setDataId(Long.valueOf(dataId));
		condition.setIsDel(BaseConstant.IS_YESNO_NO);
		paginationVo.setCondition(condition);
		
		PaginationVo searchListPage = this.splOpLogRecService.searchListPage(paginationVo);
		if (null == searchListPage) {
			return null;
		}
		
		List<SplOpLogRecDTO> datas = (List<SplOpLogRecDTO>) searchListPage.getDatas();
		if (CollectionUtils.isEmpty(datas)) {
			BaseLogUtils.newWebLogger("查询结果为空").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		} else {
			BaseLogUtils.newWebLogger("查询结果=>" + JSONObject.toJSONString(datas)).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
		}
		List<SplOpLogRecVO> voList = SplOpLogRecVOTransform.toVOList(datas);
		
		// 查询数据字典
		Map<String, String> opTypCdMap = this.sysDictionaryApiService.searchGroupToMap(SplSystemConstant.SPL_SYS_CD, 
				SplSystemConstant.DEFAULT_ORG_CD, SplOpLogRecConstant.SPL_OP_LOG_REC_DIC_CD);
		
		for (SplOpLogRecVO splOpLogRecVO : voList) {
			splOpLogRecVO.setOpTypCdNm(opTypCdMap.get(splOpLogRecVO.getOpTypCd()));
		}
		
		listPageVO.setRows(voList);
		listPageVO.setTotal(searchListPage.getTotalPage());

		return listPageVO;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView view = new ModelAndView("spl/splOpLogRec/splOpLogRecAdd");

		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("spl/splOpLogRec/splOpLogRecModify");
		view.addObject("id", id);
		return view;
	}

	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET })
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("spl/splOpLogRec/splOpLogRecDetail");
		view.addObject("id", id);

		return view;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		SplVisitRecVO entity = null;
		try {
			entity = new SplVisitRecVO();
			// TODO ADD PARAM
			Long id = 0L;//= this.splVisitRecService.add(entity);
			result.setData(id);
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("新增对象失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.newWebLogger("新增对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if (StringUtils.isEmpty(id)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			return result;
		}
		SplVisitRecVO entity = null;
		try {
			entity = new SplVisitRecVO();
			entity.setId(Long.valueOf(id));
			// entity.setName(name);
			// TODO ADD PARAM
			boolean success = false;//this.splVisitRecService.modify(entity);
			if (!success) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
			}
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("修改对象失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.newWebLogger("修改对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}

		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = { RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if (StringUtils.isEmpty(id)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			//this.splVisitRecService.removeById(Long.valueOf(id));
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.newWebLogger("移除对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if (StringUtils.isEmpty(id)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}

		try {
			//SplVisitRecVO vo = this.splVisitRecService.selectById(Long.valueOf(id));
			//if (null == vo) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
			//}
			//result.setData(vo);
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("根据主键获取对象失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.newWebLogger("根据主键获取对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}

		return result;
	}
}
